<template>
    <el-dialog title="选择法规标准" v-model="dialogOpen" @close="cancel">
        <el-scrollbar height="800">
            <el-table v-loading="loading" :data="legislationList" @selection-change="handleSelectionChange"
                ref="tableRef">
                <el-table-column type="selection" width="55" align="center" />

                <el-table-column v-for="({ title, width, selectValue }, index) in props.LCTitles" :key="index"
                    :label="title" align="center" :prop="selectValue">
                    <template #default="scope">
                        <span v-if="selectValue">
                            {{ scope.row[selectValue] }}
                        </span>
                        <el-input v-else v-model="scope.row[title]" placeholder="请输入"></el-input>
                    </template>
                </el-table-column>


                <!-- <el-table-column label="项目名称" align="center" prop="projectName" />
            <el-table-column label="要求" align="center" prop="projectRequire" />
            <el-table-column label="参考标准" align="center" prop="referenceStandards" /> -->
            </el-table>

            <!-- <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" /> -->
        </el-scrollbar>

        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitData">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup name="Legislation">
import { listLegislation, listAllLegislation, getLegislation, delLegislation, addLegislation, updateLegislation } from "@/api/flow/legislation";

const { proxy } = getCurrentInstance();

const props = defineProps({
    LCDialog: {
        type: Boolean,
        default: false
    },
    LCText: {
        type: Array,
        default: []
    },
    LCTitles: {
        type: Array,
        default: []
    }
});

const emits = defineEmits(['update:LCDialog', 'update:LCText']);

const dialogOpen = ref(props.LCDialog)
const legislationList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        projectRequire: null,
        referenceStandards: null,
    },
    rules: {
        projectName: [
            { required: true, message: "项目名称不能为空", trigger: "blur" }
        ]
    }
});

const tableRef = ref(null);
const checkedRows = ref(null);
const { queryParams, form, rules } = toRefs(data);

/** 查询法规标准管理列表 */
function getList() {
    loading.value = true;
    listAllLegislation().then(response => {
        legislationList.value = response.data;
        total.value = response.total;
        loading.value = false;

        props.LCText.forEach(item => {
            legislationList.value.forEach((element, index) => {
                if (item.id == element.id) {
                    legislationList.value[index] = JSON.parse(JSON.stringify(item));
                }
            })
        })

        // 数据加载完成后进行默认勾选操作
        if (props.LCText.length > 0) {
            nextTick(() => {
                let selectionIds = props.LCText.map(item => item.id);
                let findList = legislationList.value.filter(item => selectionIds.includes(item.id));
                findList.forEach(item => {
                    if (tableRef.value) {
                        tableRef.value.toggleRowSelection(item, true);
                    }
                });
            });
        }

    });
}


// 表单重置
function reset() {
    form.value = {
        id: null,
        projectName: null,
        require: null,
        referenceStandards: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
    };
    proxy.resetForm("legislationRef");
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;

    checkedRows.value = selection;

}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    getLegislation(_id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改法规标准";
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    console.log(row);

    // const _ids = row.id || ids.value;
    // proxy.$modal.confirm('是否确认删除法规标准管理编号为"' + _ids + '"的数据项？').then(function () {
    //     return delLegislation(_ids);
    // }).then(() => {
    //     getList();
    //     proxy.$modal.msgSuccess("删除成功");
    // }).catch(() => { });
}


function submitData() {
    emits('update:LCDialog', false);
    emits('update:LCText', checkedRows.value);
}

function cancel() {
    emits('update:LCDialog', false);
}

getList();
</script>